package com.scmt.hmzd.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.scmt.hmzd.entity.HmWxUser;
import com.scmt.hmzd.service.IHmWxUserService;
import com.scmt.core.common.vo.PageVo;
import com.scmt.core.common.vo.SearchVo;
import com.scmt.hmzd.mapper.HmWxUserMapper;
import com.scmt.core.utis.FileUtil;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.LinkedHashMap;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Map;

/**
 * @author
 **/
@Service
public class HmWxUserServiceImpl extends ServiceImpl<HmWxUserMapper, HmWxUser> implements IHmWxUserService {
    @Autowired
    @SuppressWarnings("SpringJavaAutowiringInspection")
    private HmWxUserMapper hmWxUserMapper;

    @Override
    public IPage<HmWxUser> queryHmWxUserListByPage(HmWxUser hmWxUser, SearchVo searchVo, PageVo pageVo) {
        int page = 1;
        int limit = 10;
        if (pageVo != null) {
            if (pageVo.getPageNumber() != 0) {
                page = pageVo.getPageNumber();
            }
            if (pageVo.getPageSize() != 0) {
                limit = pageVo.getPageSize();
            }
        }
        Page<HmWxUser> pageData = new Page<>(page, limit);
        QueryWrapper<HmWxUser> queryWrapper = new QueryWrapper<>();
        if (hmWxUser != null) {
            queryWrapper = LikeAllField(hmWxUser, searchVo);
        }
        IPage<HmWxUser> result = hmWxUserMapper.selectPage(pageData, queryWrapper);
        return result;
    }

    @Override
    public void download(HmWxUser hmWxUser, HttpServletResponse response) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        QueryWrapper<HmWxUser> queryWrapper = new QueryWrapper<>();
        if (hmWxUser != null) {
            queryWrapper = LikeAllField(hmWxUser, null);
        }
        List<HmWxUser> list = hmWxUserMapper.selectList(queryWrapper);
        for (HmWxUser re : list) {
            Map<String, Object> map = new LinkedHashMap<>();
            mapList.add(map);
        }
        FileUtil.createExcel(mapList, "exel.xlsx", response);
    }

    @Override
    public HmWxUser findByOpenid(String name) {
        return hmWxUserMapper.findByOpenid(name);
    }

    /**
     * 功能描述：构建模糊查询
     *
     * @param hmWxUser 需要模糊查询的信息
     * @return 返回查询
     */
    public QueryWrapper<HmWxUser> LikeAllField(HmWxUser hmWxUser, SearchVo searchVo) {
        QueryWrapper<HmWxUser> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(hmWxUser.getId())) {
            queryWrapper.and(i -> i.like("hm_wx_user.id", hmWxUser.getId()));
        }
        if (StringUtils.isNotBlank(hmWxUser.getOpenid())) {
            queryWrapper.and(i -> i.eq("hm_wx_user.openid", hmWxUser.getOpenid()));
        }
        if (StringUtils.isNotBlank(hmWxUser.getNickname())) {
            queryWrapper.and(i -> i.like("hm_wx_user.nickname", hmWxUser.getNickname()));
        }
        if (StringUtils.isNotBlank(hmWxUser.getUnionId())) {
            queryWrapper.and(i -> i.like("hm_wx_user.union_id", hmWxUser.getUnionId()));
        }
        if (hmWxUser.getCreateTime() != null) {
            queryWrapper.and(i -> i.like("hm_wx_user.create_time", hmWxUser.getCreateTime()));
        }
        if (searchVo != null) {
            if (searchVo.getStartDate() != null && searchVo.getEndDate() != null) {
                queryWrapper.lambda().and(i -> i.between(HmWxUser::getCreateTime, searchVo.getStartDate(), searchVo.getEndDate()));
            }
        }
        return queryWrapper;
    }
}
